from google.appengine.ext import webapp, gql
from google.appengine.ext.webapp.util import run_wsgi_app
from models import Business, Deal, Comment
from test.test_typechecks import Integer
import util
import string
from util import get_deal_from_xml
import logging

##
#this file main role is to handle comments adding for deals

class DataBaseRateHandler(webapp.RequestHandler):

    def get(self): 
        self.response.out.write(''' 
        <html> 
          <body> 
            <form method="post"> 
              <p>XML: <input type="string" name="xml" /></p>
              <p>Comment: <input type="string" name="comment" /></p>
              <p>User_name: <input type="string" name="user_name" /></p>
              <p>Email: <input type="string" name="email" /></p>
              <p>Rate: <input type="string" name="rate" /></p>
              <p>Cost_level: <input type="string" name="cost_level" /></p>
              <p><input type="submit" /></p>  
            </form> 
          </body> 
        </html> 
        ''') 
        
    def post(self):
        
        logging.info("Starting comment deal request")
        #get the xml
        xml = self.request.get("xml", "")
        user_name = self.request.get("user_name", "")
        email = self.request.get("email", "")
        comment_sent =  self.request.get("comment", "")
        rate =  self.request.get("rate", "")
        cost_level =  self.request.get("cost_level", "")
        deal = get_deal_from_xml(xml, produce_new=False, update_changes=False)
        
        if (deal==None):
            self.response.out.write(util.produce_result_xml("failed"))
            logging.info("Starting comment deal request: failed")
            return
        
        result = util.validate_user_credit(user_name, email)
        if (not result):
            self.response.out.write(util.produce_result_xml("failed"))
            logging.info("Starting comment deal request: failed")
            return
        
        #rate
        if (rate != ""):
            logging.info("rating")
            deal.rate_num = deal.rate_num + 1
            deal.rate_sum = deal.rate_sum +  string.atoi(rate)
        #cost level
        if (cost_level != ""):
            logging.info("cost leveling")
            deal.cost_level_num = deal.cost_level_num + 1
            deal.cost_level_sum = deal.cost_level_sum +  string.atoi(cost_level)
        
        
        logging.info("saving comment")
        
        comment = Comment()
        comment._source = email
        comment._source_type = "user"
        comment.user_name = user_name
        comment.deal_item = deal
        comment.comment_rate_num = 0
        comment.comment_rate_sum = 0
        comment.comment_str = comment_sent
        comment.comment_rate = string.atoi(rate)
        comment.comment_cost_level = string.atoi(cost_level)
        
        comment.put()
        deal.save()
        
        self.response.headers['Content-Type'] = 'text/xml'
        self.response.out.write(util.produce_result_xml("success"))
        logging.info("Starting comment deal request: sucess")


application = webapp.WSGIApplication([('/database/comment', DataBaseRateHandler)], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
